package com.itheima.listStudentServlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;

@WebServlet("/listStudent")
public class ListStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/stu.txt"), "utf-8"));
        ArrayList<Student> list = new ArrayList<>();
        String len;
        while ((len = bf.readLine()) != null) {
            String[] split = len.split(",");
            Student stu = new Student();
            stu.setName(split[0]);
            stu.setAge(Integer.parseInt(split[1]));
            stu.setScore(Integer.parseInt(split[2]));
            list.add(stu);
        }
        bf.close();
        req.setCharacterEncoding("UTF-8");
        req.setAttribute("student", list);
//        resp.sendRedirect("/stu/listStudent.jsp");
        req.getRequestDispatcher("/listStudent.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
